package com.justspeak.download;

/** 
 * copyright    Founder 2010
 * @date        2011-9-15 V1.0初始作成
 * @author      Administrator
 * @version     V1.0
 * @brief       xxxClass的定义
 * @since       1.0
 * @history     2011-9-15 V1.X 修改内容，一个版本一行
 * @descript    类的概要说明(需要手动修改)
 * @todo        未完成的事项
 */
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.justspeak.util.io.FileUtils;



/**
 * @Function：根据URL下载文件，前提是这个文件当中的内容是一个文本，函数的返回值是文件当中的内容。 1. 创建一个URL对象
 *                                                       2.通过一个URL对象创建一个HttpURLConnection对象
 *                                                       3.得到InputStream
 *                                                       4.从InputStrean中读取数据
 * @Author:Golden Zhao
 * @Version:
 * @Since:
 */
public class HttpDownloader {

    public String download(String urlStr) {
        StringBuffer sb = new StringBuffer();
        String line = null;
        BufferedReader buffer = null;
        try {
            URL url = new URL(urlStr);
            HttpURLConnection urlConn = (HttpURLConnection) url
                    .openConnection();
            buffer = new BufferedReader(new InputStreamReader(urlConn
                    .getInputStream()));
            while ((line = buffer.readLine()) != null) {
                sb.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                buffer.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

    public int downFile(String urlStr, String path, String fileName) {
        InputStream inputStream = null;
        try {
            FileUtils fileUtils = new FileUtils();

            if (fileUtils.isFileEixt(fileName, path)) {
                return 1;
            } else {
                inputStream = getInputStreamFromUrl(urlStr);
                File resultFile = fileUtils.write2SdFromInput(path, fileName,
                        inputStream);
                if (resultFile == null) {
                    return -1;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        } finally {
            try {
                if (inputStream!=null) {
                    inputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    public InputStream getInputStreamFromUrl(String urlStr)
            throws MalformedURLException, IOException {
        URL url = new URL(urlStr);
        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
        InputStream inputStream = urlConn.getInputStream();
        return inputStream;
    }
}
